<meta charset="UTF-8">
<div class="sectionsdiv">
    <?php

    include_once("../functions/db_manipulate.php");

    if (!init()) {
        redirect("login.php");
    }

    $topicSet = getTopics($_SESSION['id']);

    if ($topic = mysql_fetch_assoc($topicSet)) {
        $section = "";

        do {
            if ($topic['section'] != $section) {
                ?>
                <div class="sectionheader">
                    <?= $topic['section'] ?>
                </div>
                <?php
                $section = $topic['section'];
            }
            ?>

            <div class="sectiontopicdiv">
                <div class="sectiontopiccontentdiv" style="<?php if ($topic['uread'] == 0) {
                    echo "box-shadow: 0 0 20px rgba(255, 255, 255, 1);";
                } ?>" onclick="location.href='topic_check_read.php?tid=<?= $topic['id'] ?>'">
                    <div class="topicheader">
                        <?= $topic['header'] ?>
                    </div>
                    <div class="topicinfo">
                        <div class="topicinfodiv">
                            Автор темы: <?= $topic['author'] ?>
                        </div>
                        <div class="topicinfodiv">
                            Число создания: <?= $topic['date'] ?>
                        </div>
                        <div class="topicinfodiv">
                            Время создания: <?= $topic['time'] ?>
                        </div>
                    </div>
                    <div class="topicinfo">
                        <div class="topicinfodiv">
                            Всего сообщений: <?php $tmp = countTopicMessages($topic['id']);
                            echo $tmp; ?>
                        </div>
                        <div class="topicinfodiv">
                            Доступна группе: <?= $topic['agroup'] ?>
                        </div>
                        <div class="topicinfodiv">
                            Страницы:
                            <div id="pages">
                                <?php
                                $pages = pagesFromMessages($tmp);
                                $pagesStart = 0;
                                $pagesEnd = 0;

                                $page = $pages;

                                if ($pages <= 5) {
                                    $pagesStart = 1;
                                    $pagesEnd = $pages;
                                } else if ($pages >= 5 && $page + 2 <= $pages) {
                                    $pagesStart = $page - 2;
                                    $pagesEnd = $page + 2;
                                } else if ($pages >= 5 && $page + 2 > $pages) {
                                    $pagesStart = $pages - 4;
                                    $pages;
                                }

                                if ($pages == 0) {
                                    echo $pages;
                                }

                                if ($page > 1) {
                                    ?>
                                    <div class="page"
                                         onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=1'">
                                        <<
                                    </div>
                                <?php
                                }
                                for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                                    ?>
                                    <div class="page"
                                         onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
                                        <?= $i ?>
                                    </div>
                                <?php
                                }
                                if ($page != $pages) {
                                    ?>
                                    <div class="page"
                                         onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
                                        >>
                                    </div>
                                <?php
                                }
                                ?>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="sectiontopiclastdiv">
                    <?php
                    $lastCommentsSet = getLastComments($topic['id']);
                    while ($lastComment = mysql_fetch_assoc($lastCommentsSet)) {
                        ?>
                        <div class="lasttopiccommentinfo">
                            <div class="lasttopiccommentinfodiv">
                                Автор: <?= $lastComment['author'] ?>
                            </div>
                            <div class="lasttopiccommentinfodiv">
                                Дата: <?= $lastComment['date'] ?>
                            </div>
                            <div class="lasttopiccommentinfodiv">
                                Время: <?= $lastComment['time'] ?>
                            </div>

                        </div>

                        <div class="lasttopiccommentpart">
                            <?=
                            getTextPart($lastComment['content'], 200)?>
                        </div>

                    <?php
                    }
                    ?>
                </div>
                <div class="sectiontopicdropbutton" onclick="$('#checktopicdrop').slideDown('slow')">
                    Отписаться
                </div>

                <div id="checktopicdrop">
                    Вы действительно хотите отписаться от данной темы ?<br>

                    <div class="topicdropbutton"
                         onclick="location.href='delete_subscription.php?tid=<?= $topic['id'] ?>'">
                        Да
                    </div>
                    <div class="topicdropbutton" onclick="$('#checktopicdrop').slideUp('slow')">
                        Отмена
                    </div>
                </div>
            </div>
        <?php

        } while ($topic = mysql_fetch_assoc($topicSet));
    }


    ?>
</div>

